package xin.jishu.ai;

import lombok.extern.slf4j.Slf4j;
import xin.jishu.ai.entities.Student;
import xin.jishu.ai.managers.StudentManager;
import xin.jishu.ai.providers.BaseProvider;
import xin.jishu.ai.providers.KeyStone;
import xin.jishu.ai.providers.ProjectAdministrator;
import xin.jishu.ai.providers.Quota;

import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.atomic.LongAdder;

/**
 * @author sxsx欧克 <wo@jishu.xin>
 */
@Slf4j
public class Entrypoint {

    private static final List<BaseProvider> PROVIDERS = List.of(
            new KeyStone(), new Quota(), new ProjectAdministrator()
    );

    public static void main(String[] args) {
        // 身份提供商初始化
        Entrypoint.PROVIDERS.forEach(BaseProvider::initialize);
        //
        StudentManager manager = new StudentManager();
        //
        manager.initialize(Path.of("./source.xlsx"));
        // 解析
        List<Student> students = manager.resolve();
        // 计数器
        LongAdder counter = new LongAdder();
        // 并行处理
        students.forEach(student -> {
            counter.increment();
            //
            log.info(
                    "Processing account information of student {}, whose identity number is {}. {} items are still pending.",
                    student.getName(),
                    student.getId(),
                    students.size() - counter.sum()
            );
            //
            Entrypoint.PROVIDERS.forEach(provider -> {
                if (provider.exists(student)) {
                    log.warn(
                            "> It has an account with identity provider {}. Skip it.",
                            provider.getClass()
                                    .getSimpleName()
                    );
                } else {
                    log.info(
                            "> It has no account with identity provider {}. Create it.",
                            provider.getClass()
                                    .getSimpleName()
                    );
                    //
                    try {
                        provider.create(student);
                    } catch (Exception exception) {
                        log.error("> Oops!", exception);
                    }
                }
            });
        });
    }

}
